Skip to contentMethod: addAssertionValue(Assertion, Value)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.descriptor;
16:
17: import cz.cvut.kbss.ontodriver.model.Assertion;
18: import cz.cvut.kbss.ontodriver.model.NamedResource;
19: import cz.cvut.kbss.ontodriver.model.Value;
20: import cz.cvut.kbss.ontodriver.util.ErrorUtils;
21:
22: import java.util.*;
23:
24: public class AxiomValueDescriptor extends AxiomDescriptor {
25:
26: private final Map<Assertion, List<Value<?>>> values;
27:
28: public AxiomValueDescriptor(NamedResource subject) {
29: super(subject);
30: this.values = new HashMap<>();
31: }
32:
33: /**
34: * Adds a new value for the specified assertion. </p>
35: *
36: * @param assertion The assertion
37: * @param value The value to add
38: * @return This descriptor
39: * @throws NullPointerException if either of the arguments is {@code null}
40: */
41: public AxiomValueDescriptor addAssertionValue(Assertion assertion, Value<?> value) {
42: Objects.requireNonNull(assertion, ErrorUtils.npxMessage("assertion"));
43: Objects.requireNonNull(value, ErrorUtils.npxMessage("value"));
44:
45: final List<Value<?>> assertionValues = getAssertionList(assertion);
46: assertionValues.add(value);
47: return this;
48: }
49:
50: /**
51: * Gets values of the specified assertion held by this descriptor. </p>
52: *
53: * @param assertion The assertion
54: * @return Unmodifiable list of values or an empty list, if there are none
55: * @throws NullPointerException If the argument is {@code null}
56: */
57: public List<Value<?>> getAssertionValues(Assertion assertion) {
58: Objects.requireNonNull(assertion);
59: if (!values.containsKey(assertion)) {
60: return Collections.emptyList();
61: }
62: return Collections.unmodifiableList(values.get(assertion));
63: }
64:
65: private List<Value<?>> getAssertionList(Assertion assertion) {
66: assert assertion != null;
67: if (!values.containsKey(assertion)) {
68: values.put(assertion, new ArrayList<>());
69: addAssertion(assertion);
70: }
71: return values.get(assertion);
72: }
73:
74: @Override
75: public int hashCode() {
76: final int prime = 31;
77: int result = super.hashCode();
78: result = prime * result + values.hashCode();
79: return result;
80: }
81:
82: @Override
83: public boolean equals(Object obj) {
84: if (this == obj)
85: return true;
86: if (!super.equals(obj))
87: return false;
88: if (getClass() != obj.getClass())
89: return false;
90: AxiomValueDescriptor other = (AxiomValueDescriptor) obj;
91: if (!values.equals(other.values))
92: return false;
93: return true;
94: }
95:
96: @Override
97: public String toString() {
98: return super.toString() + ", property values: " + values;
99: }
100: }